package com.vipshop.compass.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil extends org.apache.commons.lang.time.DateUtils {
	public static int dateDiff(Date d1, Date d2) throws Exception {
		long n1 = d1.getTime();
		long n2 = d2.getTime();
		long diff = Math.abs(n1 - n2);

		diff /= 3600 * 1000 * 24;
		return (int) diff;
	}

	public static String formatDate(Date date, String pattern) {
		if (date != null) {
			String pt = "yyyy-MM-dd";
			if (pattern != null) {
				pt = pattern;
			}
			java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(pt);
			return format.format(date);
		} else {
			return "";
		}

	}

	public static Date getDate(Date date, int measure, int duration) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(measure, duration);
		return c.getTime();
	}

	public static String getFirstDayOfMonth(int year, int month) {
		month = month - 1;
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
		return new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
	}

	public static Date getFisrtDay(Date date) {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-01");
		Date fd = null;
		try {
			fd = format.parse(format.format(date));
			return fd;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return fd;
	}

	public static Date getLastDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		java.text.SimpleDateFormat formatter3 = new java.text.SimpleDateFormat("yyyy-MM-" + maxDay);
		Date ld = null;
		try {
			String lastDay = formatter3.format(cal.getTime());
			java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
			ld = format.parse(lastDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ld;
	}

	public static String getLastDayOfMonth(int year, int month) {
		month = month - 1;
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
		return new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
	}

	public static void main(String args[]) {

		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");

		Calendar c = Calendar.getInstance();

		c.add(Calendar.MONTH, -12);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));
		c.add(Calendar.MONTH, 1);
		System.out.println(format.format(getFisrtDay(c.getTime())));
		System.out.println(format.format(getLastDay(c.getTime())));

		System.out.println(format.format(getFisrtDay(new Date())));
		System.out.println(format.format(getLastDay(new Date())));

		try {
			c.setTime(format.parse("2012-04-30"));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			System.out.println(dateDiff(c.getTime(), new Date()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static Date parseDate(String date, String f) throws ParseException {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(f);
		return format.parse(date);
	}

	public int diffMinutes(Date earlier, Date later) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(earlier);
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(later);
		long diffMillis = cal2.getTimeInMillis() - cal.getTimeInMillis();
		// Get difference in seconds
		// long diffSecs = diffMillis / (1000);
		// Get difference in minutes
		long diffMins = diffMillis / (60 * 1000);
		// Get difference in hours
		// long diffHours = diffMillis / (60 * 60 * 1000);
		// Get difference in days
		// long diffDays = diffMillis / (24 * 60 * 60 * 1000);
		return Integer.parseInt(String.valueOf(diffMins));
	}
}
